<?php 
class Admin_CategorytopicController extends Zend_Controller_Action
{
	public function indexAction()
	{		
		$this->_forward('list', 'categorytopic', 'admin');
	}
	
	public function listAction()
	{
		$categories = CategoryTopic::getInstance()->getCategoryList();
		$this->view->categories = $categories;		
	}
	
	public function createAction()
	{
		$this->_helper->layout()->disableLayout();
		$this->view->categories = CategoryTopic::getInstance()->getCategoryList();
	}
	
	public function submitcreateAction()
	{
		// Disable template
		$this->_helper->layout()->disableLayout();
		$this->_helper->viewRenderer->setNoRender();
		$params = $this->_getAllParams();
		$params['active'] = 1;
		if(strlen($params['category_name']) && strlen($params['category_title'])) {
			$params['category_alias'] = My_Globals::aliasCreator($params['category_name']);
			if(!CategoryTopic::getInstance()->getCategoryByAlias($params['category_alias'])) { 
				if(CategoryTopic::getInstance()->saveCategory($params)) {
					$this->_returnState(1);
				}
			}
		}
		$this->_returnState(0);
	}
	
	public function editAction()
	{
		$this->_helper->layout()->disableLayout();
		$id = $this->_getParam('id');
		$category = CategoryTopic::getInstance()->getCategory($id);
		$categories = CategoryTopic::getInstance()->getCategoryList(null, null, null, 'category_id <> '.$id);
		$this->view->category = $category;
		$this->view->categories = $categories;
	}
	
	public function submiteditAction()
	{
		// Disable template
		$this->_helper->layout()->disableLayout();
		$this->_helper->viewRenderer->setNoRender();
		$params = $this->_getAllParams();
		if(strlen($params['category_name']) && strlen($params['category_title'])) {
			$params['category_alias'] = My_Globals::aliasCreator($params['category_name']);
			if(CategoryTopic::getInstance()->saveCategory($params)) {
					$this->_returnState(1);
			}
		}
		$this->_returnState(0);	
	}
	
	public function deleteAction()
	{
		// Disable template
		$this->_helper->layout()->disableLayout();
		$this->_helper->viewRenderer->setNoRender();
		
		$id = (int)$this->getRequest()->getParam('id');
		if( CategoryTopic::getInstance()->deleteCategory($id) ) {
			return $this->_returnState(1);
		} else {
			return $this->_returnState(0);
		}
	}
	
	public function orderAction()
	{
		$this->_helper->layout()->disableLayout();
		$this->_helper->viewRenderer->setNoRender();
		$params = $this->_getAllParams();		
		foreach($params as $k => $v) {
			$item = str_replace("order_", "", $k);
			if($item > 0 && $v >= 0) {
				$list = array(
					'category_id' => $item,
					'ordering' => $v
				);
				CategoryTopic::getInstance()->saveCategory($list);
			}
		}
		$this->_redirect('/admin/categorytopic/index');
	}
	
	public function approveAction()
	{
		$this->_helper->layout()->disableLayout();
		$this->_helper->viewRenderer->setNoRender();
		$params = $this->_getAllParams();
		if($params['category_id'] > 0) {
			$list = array(
				'category_id' => $params['category_id'],
				'active' => (($params['state'] == 0)?1:0)
			);
			if(CategoryTopic::getInstance()->saveCategory($list)) {
				return $this->_returnState(1);
			}
		}
		return $this->_returnState(0);
	}
	
	private function _returnState($state)
	{
        echo json_encode(array(
            'status' => $state
        ));
        exit();
	}
}
?>